import { BrandList, removeBrand, addUser, editUser, clientDetail,status } from "../api/coupon";
import { message } from "antd/lib/index";
import checkStatus from "@/utils/status";

export default {
  namespace: "Coupon",

  state: {
    data: {
      data: [],
      pagination: {}
    }
  },
  effects: {
    //请求用户列表
    * fetch({ payload }, { call, put }) {
      const response = yield call(BrandList, payload);
      yield checkStatus(response)
      yield put({
        type: "get",
        payload: response.msg
      });
    },
    //增加
    * add({ payload, callback }, { call, put }) {
      const response = yield call(addUser, payload);
      yield checkStatus(response)
      yield put({
        type: "save",
        payload: response
      });
      if (callback) callback(response);
    },

    * remove({ payload, callback }, { call, put }) {
      const response = yield call(removeBrand, payload);
      yield checkStatus(response)
      if (callback) callback(response);
    },

    * update({ payload, callback }, { call, put }) {
      const response = yield call(editUser, payload);
      yield checkStatus(response)
      yield put({
        type: "edit",
        payload: response
      });
      if (callback) callback();
    },
    * getDesc({ payload, callback }, { call, put }) {
      const response = yield call(clientDetail, payload);
      yield checkStatus(response)
      yield put({
        type: "getdesc",
        payload: response.msg
      });
      if (callback) callback(response);
    },



    * status({ payload, callback }, { call, put }) {
      const response = yield call(status, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },

  },

  reducers: {
    get(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total
      };
      return {
        ...state,
        data: action.payload
      };
    },
    save(state, action) {
      if (action.payload.code === 200) {
        message.success("添加成功");
        return {
          ...state,
          data: action.payload.msg
        };
      } else {
        message.error(action.payload.errMsg);
        return {
          ...state,
          data: []
        };
      }

    },

    edit(state, action) {
      if (action.payload.code === 200) {
        message.success("更新成功");
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload
      };
    },

    getdesc(state, action) {
      return {
        ...state,
        Desc: action.payload
      };
    },


  }
};
